package com.service.healthycode.config;


import com.service.config.annotation.DistinctSmsCode;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.RegexUtil;
import com.service.config.utils.StringUtils;
import com.service.healthycode.dto.SmsCode;
import com.service.healthycode.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import static com.service.config.constant.Constant.DATA_FORMAT_WRONG;
import static com.service.config.constant.Constant.INCORRECT_DATA_FORMAT;

/**
 * @ClassName: PreHandler
 * @Description: 拦截器
 * @Author guochuang
 * @Date 2021/10/19
 * @Version 1.0
 */
@Slf4j
public class SmsCodeHandler implements HandlerInterceptor {
    private static final Logger LOG = LoggerFactory.getLogger(SmsCodeHandler.class);
    private
    @Autowired
    RedisUtil redisUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断是否存在注解
        if (!(handler instanceof HandlerMethod)) {
            LOG.info("不是HandlerMethod1类型，则无需检查");
            return true;
        }
        HandlerMethod method = (HandlerMethod) handler;
        boolean flag = method.getMethod().isAnnotationPresent(DistinctSmsCode.class);
        if (!flag) {
            //不存在Distinct注解，则直接通过
            LOG.info("不存在DistinctSmsCode注解，则直接通过");
            return true;
        }else {
            RequestWrapperAuthorization requestWrapper = new RequestWrapperAuthorization(request);
            String body = requestWrapper.getBody() ;
            SmsCode requestBodEntity = JsonXMLUtils.json2obj(body, SmsCode.class);
            if (StringUtils.isEmpty(requestBodEntity.getTelephone())) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                PrintWriter out = null ;
                Map map = new HashMap();
                map.put("code",DATA_FORMAT_WRONG);
                map.put("msg",INCORRECT_DATA_FORMAT);
                out = response.getWriter();
                out.append(JsonXMLUtils.obj2json(map));
                return false;
            }
            if (!RegexUtil.isMobileSimple(requestBodEntity.getTelephone())) {
                PrintWriter out = null ;
                Map map = new HashMap();
                map.put("code",DATA_FORMAT_WRONG);
                map.put("msg",INCORRECT_DATA_FORMAT);
                out = response.getWriter();
                out.append(JsonXMLUtils.obj2json(map));
                return false;
            }
            if (!redisUtil.exists(requestBodEntity.getCode())) {
                PrintWriter out = null ;
                Map map = new HashMap();
                map.put("code",403);
                map.put("msg","验证码已失效，请重新发送");
                out = response.getWriter();
                out.append(JsonXMLUtils.obj2json(map));
                return false;
            }else {
                String value = redisUtil.getValue(requestBodEntity.getCode());
                UserInfo userInfo = JsonXMLUtils.json2obj(value, UserInfo.class);
                if (!requestBodEntity.getTelephone().equals(userInfo.getTelephone())) {
                    PrintWriter out = null ;
                    Map map = new HashMap();
                    map.put("code",403);
                    map.put("msg","手机号不一致，请校验手机号");
                    out = response.getWriter();
                    out.append(JsonXMLUtils.obj2json(map));
                    return false;
                }
                return true;
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { }
}
